package  com.zzkj.util
{

	
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class LoadXmlUtil
	{
		public function LoadXmlUtil()
		{
		}
		private static var p:String;
		public static function loadXml(path:String,fn:Function,format:String=null):void{
			p = path;

			var httpService:HTTPService = new HTTPService();
				httpService.url = path;
				if(format==null)
					httpService.resultFormat = "e4x";
				else
					httpService.resultFormat = format;
				httpService.addEventListener(FaultEvent.FAULT, onFaultHttpService);
				httpService.addEventListener(ResultEvent.RESULT, fn);
				httpService.send();
		}
		private static function onFaultHttpService(e:FaultEvent):void{
				Alert.show("Unable to load xml:"+ p +"\n"+ e.message);
		}
		
		
		//根据权限初始化菜单
		public static function transToXMLList(xmllist:XMLList,dic:Dictionary):XMLListCollection{
			var nlist:XMLListCollection = new XMLListCollection();
			var menuXML:XMLList;
			for(var i:Number=0;i<xmllist.length();i++){
				var xml:XML = xmllist[i];
				//菜单名
				var menuName:String = xml.@label;
				if(dic[menuName]!=null){
					menuXML = new XMLList("<menu label='"+menuName+"' level='0'></menu>");
					//子菜单集合
					var c:ArrayCollection = dic[menuName];
					//xml子菜单
					var nl:XMLList = xml.children();
					for(var k:Number =0;k<c.length;k++){
						var cName:String = c[k];//子菜单
						for(var m:Number=0;m<nl.length();m++){
							var xmltemp:XML = nl[m];
							//xml子菜单
							var xmlcName:String = xmltemp.@label;							
							if(cName===xmlcName){
								menuXML.appendChild(xmltemp);
							}
						}
					}
					nlist.addItem(menuXML);
				}
			}
			return nlist;
		}

	}
}